import {
    reqCart,
    reqBuyNowGoodsInfo,
    reqAdress,
    reqSubmit,
    reqPayInfo,
    reqPayStatus
} from '../../api/index'
import moment from 'moment';
//订单详情页
Page({
    data: {
        goodsId: 0, //存储立即购买而来的商品的ID
        remarks: '', //存储立即购买的携带祝福语
        cartArr: [], //展示商品的数组
        total: 0, //存储商品的总价
        userInfo: {}, //收货人的地址
        buyName: '', //收集订购人的名字
        buyPhone: '', //订购人手机号码
        deliveryDate: '', //期望送达日期
        isShowPop: false, //控制pop弹窗显示与隐藏
        minDate: new Date().getTime(), //日期显示最小的时间
    },
    onLoad(options) {
        //携带祝福语:立即购买而来
        if (options.remarks) {
            //存储祝福语与立即购买商品的ID
            this.setData({
                goodsId: options.goodsId,
                remarks: options.remarks
            });
            //获取立即购买商品的信息
            this.getBuyGoodsInfo()

        } else {
            //没有祝福语一定是购物车
            this.getCartList();
        }
    },
    onShow() {
        //不管是购物车而来、还是立即购买而来都需要收货人的地址
        this.getAddress();
    },
    //获取用户购物车的数据
    async getCartList() {
        const result = await reqCart();
        if (result.code == 200) {
            //存储购物车的数据
            this.setData({
                cartArr: result.data
            });
            //获取购物车数据完毕以后计算总价
            this.computedTotal();
        }
    },
    //计算购物车总价
    computedTotal() {
        const total = this.data.cartArr.reduce((prev, next) => {
            return prev += next.count * next.price;
        }, 0);
        //更新总价
        this.setData({
            total
        })
    },
    //获取立即购买商品的信息
    async getBuyGoodsInfo() {
        const {
            goodsId,
            remarks
        } = this.data;
        //获取立即购买的商品的信息
        const result = await reqBuyNowGoodsInfo(goodsId, remarks);
        if (result.code == 200) {
            this.setData({
                cartArr: result.data.cartVoList, //商品信息
                total: result.data.totalAmount, //立即购买商品总价
            })
        }
    },
    async getAddress() {
        const result = await reqAdress();
        if (result.code == 200) {
            this.setData({
                userInfo: result.data
            })
        }
    },
    //结算按钮的提交订单的方法
    async submitOrder() {
        //整理参数
        const data = {
            userAddressId: this.data.userInfo.id, //收货人地址的ID
            buyName: this.data.buyName, //订购人姓名
            buyPhone: this.data.buyPhone, //订购人手机号码
            deliveryDate: this.data.deliveryDate, //期望送达日期
            remarks: this.data.remarks, //祝福语
            cartList: this.data.cartArr, //商品数组
        }
        //提交订单
        const result = await reqSubmit(data);
        if (result.code == 200) {
            //获取支付参数信息
            this.getPayParams(result.data);
        }
    },

    //获取wx支付相关的参数
    async getPayParams(orderNo) {
        //获取支付参数
        const result = await reqPayInfo(orderNo);
        console.log(result);
        if (result.code == 200) {
            //发起微信支付:你们没有办法真机测试
            wx.requestPayment({
                nonceStr: result.data.nonceStr,
                package: result.data.package,
                paySign: result.data.paySign,
                timeStamp: result.data.timeStamp,
                signType: result.data.signType,
                //支付成功的回调
                async success() {
                    //询问服务器支付的结果在进行下一步处理
                    const result = await reqPayStatus(orderNo);
                    if (result.code == 200) {
                        wx.navigateTo({
                            url: '/pages/order/paySuccess/paysuccess',
                        })
                    }
                }
            })
        }
    },

    //点击期望送达日期弹出pop
    showPop() {
        this.setData({
            isShowPop: true
        })
    },
    //遮罩层与取消
    close() {
        this.setData({
            isShowPop: false
        })
    },
    //收集确定的预期时间
    confirm(event) {
        this.setData({
            deliveryDate: moment(event.detail).format('YYYY-MM-DD'),
            isShowPop: false
        })
    }

})